<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/9
 * Time: 15:35
 */
if (!defined('ACCESS')) {
    exit('Access denied.');
}

class Products extends DBBase
{

    private static $table_name = 'products';
    // 查询字段
    private static $columns = array(
        'products_id',
        'products_model',
        'products_name',
        'products_images',
        'products_small_images',
        'products_description',
        'products_price',
        'products_special_price',
        'products_currency',
        'products_desc',
        'products_color',
        'products_size',
        'products_sale_num',
        'products_sale_other',
        'products_index',
        'products_add_date');

    public static function getTableName()
    {
        return self::$table_name;
    }


    public static function getProductById($pid)
    {
        $condition = array("products_id" => $pid);
        $db = self::__instance();
        $product = $db->get(self::getTableName(), self::$columns, $condition);
        if ($product) {
            return $product;
        }
        return array();
    }


    public static function count($pid)
    {
        $condition = array("products_id" => $pid);
        $db = self::__instance();
        $product = $db->get(self::getTableName(), self::$columns, $condition);
        if ($product) {
            return $product;
        }
        return array();
    }


    public static function getDefProduct()
    {
        $condition = array("products_index" => '1');
        $db = self::__instance();
        $products = $db->select(self::getTableName(), self::$columns, $condition);

        if ($products) {
            return $products;
        }
        return array();
    }


    public static function getDefProdImages()
    {
        $condition = array("products_index" => '1');
        $db = self::__instance();
        $products = $db->get(self::getTableName(), self::$columns, $condition);

        if ($products) {
            $imgs = $products['products_images'];
            $def_img = explode("||", $imgs);
            foreach($def_img as $img){
                $img_ts = parse_url($img, PHP_URL_HOST);
                if(!$img_ts){
                    $def_imgs[] = COLOR_IMG.$img;
                }else{
                    $def_imgs[] = $img;
                }
            }
            return $def_imgs;
        }
        return array();
    }
}